import QtQuick 1.1
import org.kde.plasma.core 0.1 as PlasmaCore
import org.kde.plasma.components 0.1 as PlasmaComponents
import org.kde.plasma.extras 0.1 as PlasmaExtras
import org.kde.qtextracomponents 0.1 as QtExtraComponents

Item {
    id: taskContent
    
    property alias icon: iconDelegate.icon
    property alias label: labelDelegate.label
    property alias source:controlsDelegate.source
    property alias maximized: controlsDelegate.maximizedWindow

    property string orientation // TODO: replace with enum; now accept "UP", "LEFT" and "RIGHT"
    property variant margins
    property bool showLabel: true
    property bool showControls: true
    
    property bool squareLayout: true
    property bool rowLayout: true
    property bool columnLayout: true
    
    property int spacing: Math.max(Math.min(margins.left, margins.right,margins.top, margins.bottom),2)
    
    anchors.leftMargin: margins.left
    anchors.rightMargin: margins.right
    anchors.topMargin: margins.top
    anchors.bottomMargin: margins.bottom
    
    IconContent {
        id: iconDelegate
        
        minimumWidth: labelDelegate.minimumHeight
        minimumHeight: labelDelegate.minimumHeight
        show: false
    }    
    
    LabelContent {
        id: labelDelegate
        
        alignment: Text.AlignHCenter
        show: false
    }
    
    ButtonsContent {
        id: controlsDelegate
        
        minimumButtonsSize: labelDelegate.minimumHeight
        maximizedWindow: taskContent.maximized
        spacing: taskContent.spacing
        show: false   
    }
    
    states: [
        State {
            name: "SQUARE_LABEL_CONTROLS"
            when: taskContent.showLabel && taskContent.showControls && ( ( !taskContent.rowLayout && !taskContent.columnLayout) || taskContent.squareLayout) && ( taskContent.width >= Math.max(labelDelegate.minimumWidth,iconDelegate.minimumWidth/*,controlsDelegate.minimumWidth*/)  && taskContent.height >= Math.max(iconDelegate.minimumHeight/*,controlsDelegate.minimumHeight*/)+taskContent.spacing+labelDelegate.minimumHeight)
            
            PropertyChanges {
                target: iconDelegate
                
                show: true
                
                width: height
                height: Math.min(desiredHeight, parent.height-labelDelegate.minimumHeight-taskContent.spacing)
            }
            
            PropertyChanges {
                target: labelDelegate

                show: true
                alignment: Text.AlignHCenter

                width: undefined
                height: undefined
                anchors.topMargin: taskContent.spacing
            }
             
             PropertyChanges {
                target: controlsDelegate
                
                show: true

                anchors.leftMargin: taskContent.spacing
                anchors.bottomMargin: taskContent.spacing
                height: undefined
                width:undefined
            }
            
            AnchorChanges {
                target: iconDelegate
                
                anchors.bottom: undefined
                anchors.left: parent.left
                anchors.right: undefined
                anchors.top: parent.top                
            }
            
            AnchorChanges {
                target: labelDelegate
                
                anchors.bottom: parent.bottom
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.top: iconDelegate.bottom
            }
            
            AnchorChanges {
                target: controlsDelegate
                
                anchors.top: parent.top
                anchors.right: parent.right
                anchors.bottom: labelDelegate.top
                anchors.left: iconDelegate.right
            }
        }
        ,
        State {
            name: "ROW_LABEL_CONTROLS"
            when: taskContent.showLabel && taskContent.showControls && ( ( !taskContent.columnLayout && !taskContent.squareLayout) || taskContent.rowLayout) && (taskContent.width >= iconDelegate.minimumWidth+labelDelegate.minimumWidth+controlsDelegate.minimumWidth+taskContent.spacing*2 && taskContent.height >= Math.max(iconDelegate.minimumHeight, labelDelegate.minimumHeight, controlsDelegate.minimumHeight))
            
            PropertyChanges {
                target: iconDelegate
                
                show: true
                
                width: height
                height: undefined
                anchors.leftMargin: taskContent.spacing
                anchors.topMargin: taskContent.spacing
                anchors.bottomMargin: taskContent.spacing
            }
            
            PropertyChanges {
                target: labelDelegate
                
                show: true
                alignment: Text.AlignLeft
                
                width: undefined
                height: undefined
                anchors.rightMargin: taskContent.spacing
                anchors.leftMargin: taskContent.spacing
                anchors.topMargin: taskContent.spacing
                anchors.bottomMargin: taskContent.spacing    
            }
             
             PropertyChanges {
                target: controlsDelegate
                
                width: 100
                height: undefined
                anchors.rightMargin: taskContent.spacing
                anchors.leftMargin: taskContent.spacing
                anchors.topMargin: taskContent.spacing
                anchors.bottomMargin: taskContent.spacing  
                show: true
            }
            
            AnchorChanges {
                target: iconDelegate
                
                anchors.left: parent.left
                anchors.right: undefined
                anchors.top: parent.top
                anchors.bottom: parent.bottom
            }
            
            AnchorChanges {
                target: labelDelegate
                
                anchors.left: iconDelegate.right
                anchors.right: controlsDelegate.left
                anchors.top: parent.top
                anchors.bottom: parent.bottom
            }
            
            AnchorChanges {
                target: controlsDelegate
                                
                anchors.left: undefined
                anchors.right: parent.right
                anchors.top: parent.top
                anchors.bottom: parent.bottom
            }            
        }
        ,
        State {
            name: "COLUMN_LABEL_CONTROLS"
            when: taskContent.showLabel && taskContent.showControls && ( ( !taskContent.rowLayout && !taskContent.squareLayout) || taskContent.columnLayout) && (taskContent.width >= Math.max(iconDelegate.minimumWidth,labelDelegate.minimumHeight,controlsDelegate.minimumWidth) && taskContent.height >= iconDelegate.minimumHeight+labelDelegate.minimumWidth+controlsContent.minimumHeight+taskContent.spacing*2)
            
            PropertyChanges {
                target: iconDelegate
                
                show: true
            }
            
            PropertyChanges {
                target: labelDelegate
                
                show: true
                alignment: Text.AlignLeft
            }
             
             PropertyChanges {
                target: controlsDelegate
                
                show: true
            }
            
            AnchorChanges {
                target: iconDelegate
            }
            
            AnchorChanges {
                target: labelDelegate
            }
            
            AnchorChanges {
                target: controlsDelegate
            }            
        }
        ,
        State {
            name: "SQUARE_CONTROLS"
            when: taskContent.showControls && ( ( !taskContent.rowLayout && !taskContent.columnLayout) || taskContent.squareLayout) && ( taskContent.width >= Math.max(iconDelegate.minimumWidth,controlsDelegate.minimumWidth)  && taskContent.height >= Math.max(iconDelegate.minimumHeight,controlsDelegate.minimumHeight))
            
            PropertyChanges {
                target: iconDelegate
                
                show: true
            }
            
            PropertyChanges {
                target: labelDelegate
                
                show: false
            }
             
             PropertyChanges {
                target: controlsDelegate
                
                show: true
            }
            
            AnchorChanges {
                target: iconDelegate
            }
            
            AnchorChanges {
                target: controlsDelegate
            }            
        }
        ,
        State {
            name: "ROW_CONTROLS"
            when: taskContent.showControls && ( ( !taskContent.columnLayout && !taskContent.squareLayout) || taskContent.rowLayout) && (taskContent.width >= iconDelegate.minimumWidth+controlsDelegate.minimumWidth+taskContent.spacing && taskContent.height >= Math.max(iconDelegate.minimumHeight, controlsContent.minimumHeight))
            
            PropertyChanges {
                target: iconDelegate
                
                show: true
            }
            
            PropertyChanges {
                target: labelDelegate
                
                show: false
            }
             
             PropertyChanges {
                target: controlsDelegate
                
                show: true
            }
            
            AnchorChanges {
                target: iconDelegate
            }

            AnchorChanges {
                target: controlsDelegate
            }            
        }
        ,
        State {
            name: "COLUMN_CONTROLS"
            when: taskContent.showControls && ( ( !taskContent.rowLayout && !taskContent.squareLayout) || taskContent.columnLayout) && (taskContent.width >= Math.max(iconDelegate.minimumWidth,controlsDelegate.minimumWidth) && taskContent.height >= iconDelegate.minimumHeight+controlsContent.minimumHeight+taskContent.spacing)
            
            PropertyChanges {
                target: iconDelegate
                
                show: true
            }
            
            PropertyChanges {
                target: labelDelegate
                
                show: false
            }
             
             PropertyChanges {
                target: controlsDelegate
                
                show: true
            }
            
            AnchorChanges {
                target: iconDelegate
            }
            
            AnchorChanges {
                target: controlsDelegate
            }            
        }
        ,
        State {
            name: "SQUARE_LABEL"
            when: taskContent.showLabel && ( ( !taskContent.rowLayout && !taskContent.columnLayout) || taskContent.squareLayout) && ( taskContent.width >= Math.max(iconDelegate.minimumWidth,labelDelegate.minimumWidth)  && taskContent.height >= iconDelegate.minimumHeight+labelDelegate.minimumHeight+taskContent.spacing)
            
            PropertyChanges {
                target: iconDelegate
                
                show: true
            }
            
            PropertyChanges {
                target: labelDelegate
                
                show: true
                alignment: Text.AlignHCenter
            }
             
             PropertyChanges {
                target: controlsDelegate
                
                show: false
            }
            
            AnchorChanges {
                target: iconDelegate
            }
            
            AnchorChanges {
                target: labelDelegate
            }
        }
        ,
        State {
            name: "ROW_LABEL"
            when: taskContent.showLabel && ( ( !taskContent.columnLayout && !taskContent.squareLayout) || taskContent.rowLayout) && (taskContent.width >= iconDelegate.minimumWidth+labelDelegate.minimumWidth+taskContent.spacing && taskContent.height >= Math.max(iconDelegate.minimumHeight, labelContent.minimumHeight))
            
            PropertyChanges {
                target: iconDelegate
                
                show: true
            }
            
            PropertyChanges {
                target: labelDelegate
                
                show: true
                alignment: Text.AlignLeft
            }
             
             PropertyChanges {
                target: controlsDelegate
                
                show: false
            }
            
            AnchorChanges {
                target: iconDelegate
            }
            
            AnchorChanges {
                target: labelDelegate
            }           
        }
        ,
        State {
            name: "COLUMN_LABEL"
            when: taskContent.showLabel && ( ( !taskContent.rowLayout && !taskContent.squareLayout) || taskContent.columnLayout) && (taskContent.width >= Math.max(iconDelegate.minimumWidth,labelDelegate.minimumHeight) && taskContent.height >= iconDelegate.minimumHeight+labelContent.minimumWidth+taskContent.spacing)
            
            PropertyChanges {
                target: iconDelegate
                
                show: true
            }
            
            PropertyChanges {
                target: labelDelegate
                
                show: true
                alignment: Text.AlignLeft
            }
             
             PropertyChanges {
                target: controlsDelegate
                
                show: false
            }
            
            AnchorChanges {
                target: iconDelegate
            }
            
            AnchorChanges {
                target: labelDelegate
            }          
        }
        ,  
        State {
            name: "ICON_ONLY"
            when: taskContent.width >= iconDelegate.minimumWidth && taskContent.height >= iconDelegate.minimumHeight
            
            PropertyChanges {
                target: iconDelegate
                
                show: true
            }
            
            PropertyChanges {
                target: labelDelegate
                
                show: false
            }
             
             PropertyChanges {
                target: controlsDelegate
                
                show: false
            }
            
            AnchorChanges {
                target: iconDelegate
            }
        }
    ]
    
//     Text {
//         id: debug
//         
//         text: taskContent.state+"\n"+(taskContent.showLabel && taskContent.showControls && ( ( !taskContent.columnLayout && !taskContent.squareLayout) || taskContent.rowLayout))+"&"+((taskContent.width >= iconDelegate.minimumWidth+labelDelegate.minimumWidth+controlsDelegate.minimumWidth+taskContent.spacing*2 && taskContent.height >= Math.max(iconDelegate.minimumHeight, labelDelegate.minimumHeight, controlsDelegate.minimumHeight)))
//         anchors.fill: parent
//     }
}            
    
